﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;

namespace dhDataAcess.orderDAL
{
   public  class productListDAL
    {
       string  tableName = "productList";
        private SqlTransaction tran = null;
        private SqlConnection conn = null;
        public productListDAL(SqlTransaction _transaction, SqlConnection _conn)
        {
            tran = _transaction ;
            conn = _conn;
        }
        /// <summary>
        /// 插入产品信息表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>
        public int productListInsert(productListModel row)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string insertstr = baseBLL.dTableColumnRead.insertSqlStr(tableName).ToString();
            SqlParameter[] para = {
                                    new SqlParameter ("@mark",row .mark ),
                                    new SqlParameter ("@updateUser",row .updateUser),
                                    new SqlParameter ("@updateDate",row .updateDate),
                                    new SqlParameter ("@createUser",row .createUser),
                                    new SqlParameter ("@createDate",row .createDate),
                                     new SqlParameter ("@grwight",row .grwight ),
                                    new SqlParameter ("@pattr",row .pattr),
                                    new SqlParameter ("@pmode",row .pmode ),
                                    new SqlParameter ("@ptype",row .ptype ),
                                    new SqlParameter ("@pnameMark",row .pnameMark),
                                    new SqlParameter ("@pname",row .pname),
                                     new SqlParameter ("@stockQty","0" ),
                                     new SqlParameter ("@packwight","0" ),
                                     new SqlParameter ("@stockQtyQC","0" ),
                                     new SqlParameter ("@stockposition",row .stockposition  ),
                                     new SqlParameter ("@qcStatus",row .qcStatus   )

                                  };
            cmd.Parameters.AddRange(para);
            cmd.CommandText = insertstr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 导入原材料产品表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>
        public int productimportInsert(orderDataSet.productListDataTable  prodcutTable)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter par = new SqlParameter("@product", prodcutTable);
            par.SqlDbType = SqlDbType.Structured;
            par.TypeName = "dbo.productQC";


            cmd.Parameters.Add(par);
            cmd.CommandText = "productQC";

            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 更新产品信息表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>
        public int productListUpdate(productListModel row)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string updatestr = "update productList set mark=@mark,updateUser=@updateUser,updateDate=@updateDate,createUser=@createUser,createDate=@createDate,pattr=@pattr,pmode=@pmode,ptype=@ptype,pnameMark=@pnameMark,pname=@pname,qcStatus=@qcStatus,stockposition=@stockposition where recordid=@recordid";
          // string updatestr = baseBLL.dTableColumnRead.updateSqlStr(tableName).ToString();
            SqlParameter[] para = {
                                    new SqlParameter ("@mark",row .mark ),
                                    new SqlParameter ("@updateUser",row .updateUser),
                                    new SqlParameter ("@updateDate",row .updateDate),
                                    new SqlParameter ("@createUser",row .createUser),
                                    new SqlParameter ("@createDate",row .createDate),
                                     new SqlParameter ("@grwight",row .grwight ),
                                    new SqlParameter ("@pattr",row .pattr),
                                    new SqlParameter ("@pmode",row .pmode ),
                                    new SqlParameter ("@ptype",row .ptype ),
                                    new SqlParameter ("@pnameMark",row .pnameMark),
                                    new SqlParameter ("@pname",row .pname),
                                    new SqlParameter ("@recordid",row .recordid ),
                                    new SqlParameter ("@stockposition",row .stockposition  ),
                                     new SqlParameter ("@qcStatus",row .qcStatus   )
                                  };
            cmd.Parameters.AddRange(para);
            cmd.CommandText = updatestr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 删除一行产品信息表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>
        public int productListDel(string pname)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string deletestr = baseBLL.dTableColumnRead.delSqlStr (tableName).ToString();
            SqlParameter[] para = {
                                
                                    new SqlParameter ("@pname",pname)
                                  }; 
            cmd.Parameters.AddRange(para);
            cmd.CommandText = deletestr ;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        //public SqlDataReader productListretquery(List<queryModel> queryList)
        //{
        //    SqlCommand cmd = new SqlCommand();
        //    cmd.Connection = conn;
        //    cmd.CommandType = CommandType.Text;
        //    string selectStr = "select * from productList where ";

        //    int i = 0;
        //    foreach (queryModel query in queryList)
        //    {
        //        if (i == 0)
        //        {
        //            selectStr = selectStr + query.strField + query.strBJ + "@" + query.strFieldParm;
        //        }
        //        else
        //        {
        //            selectStr = selectStr + " or " + query.strField + query.strBJ + "@" + query.strFieldParm;
        //        }
        //        i++;
        //    }



        //    foreach (queryModel query in queryList)
        //    {
        //        SqlParameter parm = new SqlParameter("@" + query.strFieldParm, query.strValue);
        //        cmd.Parameters.Add(parm);
        //    }

        //    cmd.CommandText = selectStr;
        //    cmd.Transaction = tran;
        //    SqlDataReader rdr = cmd.ExecuteReader();


        //    return rdr;
        //}
    }
}
